<template>
  <GlobalContainer className="!bg-[#F3F3F3]" :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`">
    <GlobalHeader ref="headerRef" title="历史工单" :fixed="true" showBack :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`" />
    <div class="px-24">
      <div class="status-bar-container fixed" :style="{ top: `${headerHeight}` }">
        <div
          class="status-bar-item"
          v-for="(item, index) in statusBarList"
          :key="index"
          :class="statusBarActive === item.value && 'status-bar-item-active'"
          @tap="statusBarChange(item.value)"
        >
          {{ item.name }}
        </div>
      </div>

      <div class="jieyong-list" :style="{ paddingTop: '100rpx' }">
        <div class="jieyong-item mb-20 p-32 pr-44" v-for="item in renovationApplyList" :key="item.id" @tap="toPageHandle(item.id)">
          <div class="line1">
            <span class="text-30 leading-45 text-[#303133]">装修申请</span>
            <div class="status ml-auto" :class="getStatusClass(item.status)">{{ getStatusText(item.status) }}</div>
            <image class="ml-20 size-32" src="../../static/images/jiaofei_right_icon.png" />
          </div>
          <div class="line2 text-28 leading-42 mt-12 text-[#303133]">
            {{ accountStore.roomInfo }}
          </div>
          <div v-if="item.reason" class="mt-12">
            <span class="text-28 leading-42 text-[#303133]">拒绝原因：</span>
            <span class="text-28 mt-24 break-all text-justify font-normal leading-[1.4] text-[#909399]">
              {{ item.reason }}
            </span>
          </div>
          <div class="line3">
            <span class="text-28 leading-42 mt-12 font-normal text-[#909399]">{{ item.createTime }}</span>
          </div>
        </div>
      </div>
    </div>

    <div v-if="renovationApplyList.length === 0" class="empty-container mt-240 mx-auto flex flex-col items-center">
      <div class="empty-img mb-40">
        <image class="h-240 w-330" src="../../static/images/jiaofei_empty.png" />
      </div>
      <div class="empty-text text-28 leading-36 text-[#909399]">暂无记录</div>
    </div>
    <div v-else class="empty-container text-28 leading-36 pb-20 text-center text-[#909399]">已加载全部记录</div>
  </GlobalContainer>
</template>

<script setup>
import { ref, computed } from 'vue'
import { getRenovationApplyRecord } from '@/common/api/report'
import { showLoading, hideLoading } from '@/utils'
import { useAccountStore } from '@/store'

const OSS_URL = import.meta.env.VITE_OSS_URL
const accountStore = useAccountStore()

const headerRef = ref()
const headerHeight = computed(() => {
  return headerRef.value?.wrapStyle?.height
})
const statusBarList = ref([
  {
    name: '全部',
    value: ''
  },
  {
    name: '待审核',
    value: 0
  },
  {
    name: '通过',
    value: 1
  },
  {
    name: '拒绝',
    value: 2
  }
])

const getStatusClass = status => {
  switch (status) {
    case 0:
      return 'status-wait'
    case 1:
      return 'status-pass'
    case 2:
      return 'status-fail'
    default:
      return 'status-wait'
  }
}

const getStatusText = status => {
  switch (status) {
    case 0:
      return '待审核'
    case 1:
      return '已通过'
    case 2:
      return '已拒绝'
    default:
      return '待审核'
  }
}
const statusBarActive = ref('')
const statusBarChange = active => {
  statusBarActive.value = active
  getRenovationApplyPage(active)
}
const renovationApplyList = ref([])
const getRenovationApplyPage = active => {
  showLoading()
  getRenovationApplyRecord({
    pageNo: 1,
    pageSize: 9999,
    status: active
  })
    .then(res => {
      console.log('[ res ] >', res)
      renovationApplyList.value = res.records || []
    })
    .finally(() => {
      hideLoading()
      uni.stopPullDownRefresh()
    })
}
getRenovationApplyPage(statusBarActive.value)

const toPageHandle = id => {
  uni.navigateTo({
    url: `/subPages/zhuangxiuDetail/index?id=${id}`
  })
}

onPullDownRefresh(() => {
  getRenovationApplyPage(statusBarActive.value)
})
</script>

<style scoped lang="scss">
.status-bar-container {
  width: 702rpx;
  left: 50%;
  transform: translateX(-50%);
  height: 80rpx;
  line-height: 1;
  text-align: center;
  background: #ffffff;
  border-radius: 10rpx 10rpx 10rpx 10rpx;
  padding: 24rpx 0;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.status-bar-item {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
  font-size: 28rpx;
  color: #606266;
  border-right: 1px solid #e3e7ec;
}
.status-bar-item-active {
  color: #1e90ff;
}

.jieyong-list {
  .jieyong-item {
    background: #fcfeff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    border: 1rpx solid #ffffff;
    .line1,
    .line2,
    .line3 {
      display: flex;
      align-items: center;
    }
  }
}
.status {
  width: 118rpx;
  height: 52rpx;
  text-align: center;
  line-height: 52rpx;
  font-weight: 400;
  font-size: 26rpx;
  color: #ffffff;
  box-shadow: inset 0rpx 8rpx 16rpx 0rpx rgba(255, 255, 255, 0.3);
  border-radius: 12rpx 0rpx 12rpx 0rpx;
}
.status-wait {
  background: linear-gradient(303deg, #ff9933 0%, #ff8000 100%);
}
.status-pass {
  background: linear-gradient(303deg, #2bd982 0%, #26bf73 100%);
}
.status-fail {
  background: linear-gradient(303deg, #ff5533 0%, #ff3333 100%);
}
</style>
